<?php 
    $type = pc_variable("type");
    $typeName = pc_variable("typeName");
    $typeConfiguration = pc_config("cm.type." . $type->getFullName());
    $attributeConfigurationManager = pc_config("cm.attributeConfigurationManager");
    $attributeCount = 0;
?>
<h2><?php pc_echo(pc_variable("title")); ?></h2>
<form action="<?php pc_echo(pc_link()); ?>" method="get">
    <input type="hidden" name="typeName" value="<?php pc_echo($typeName); ?>"/>
    <table>
        <thead>
            <tr>
                <th> </th>
            <?php 
                foreach($type->getAttributes() as $name => $attribute) {
                    $attributeConfiguration = $attributeConfigurationManager->getAttributeConfiguration($attribute);
                    if($attributeConfiguration === null || $attributeConfiguration->getListView() !== null) {
                        $attributeCount++;
            ?>
                <th><?php pc_echo(pc_message("field." . $name)); ?></th>
            <?php 
                    }
                }
            ?>
            </tr>
        </thead>
        <tbody>
            <?php $entities = pc_variable("entities");
                  if($entities->isEmpty()) { ?><tr>
                      <td colspan="<?php pc_echo($attributeCount + 1); ?>">
                          <?php pc_echo(pc_message("listEntities.empty", array(Pageley_Util_String::uncapitalize(pc_message("namespace." . $typeName . ".plural"))))); ?>
                      </td>
                  </tr><?php }
                  foreach($entities as $entity) { ?>
                <tr>
                    <td><input type="checkbox" name="ids[]" id="list-item<?php pc_echo($entity->getId()); ?>" value="<?php pc_echo($entity->getId()); ?>"/></td>
                    <?php 
                        foreach($type->getAttributes() as $name => $attribute) {
                            $attributeConfiguration = $attributeConfigurationManager->getAttributeConfiguration($attribute);
                            if($attributeConfiguration === null || $attributeConfiguration->getListView() !== null) { 
                    ?>
                        <td><?php 
                            if($name == $typeConfiguration->getPrimaryPropertyName()) {
                                pc_pagelet("list-entities/attribute/reference", 
                                    array(
                                        "attribute" => $attribute,
                                        "name" => $name,
                                        "value" => $entity
                                    ));
                            } else {
                                pc_pagelet("list-entities/attribute/switch", 
                                    array(
                                        "attribute" => $attribute,
                                        "name" => $name,
                                        "value" => $entity->get($name)
                                    ));
                            }
                        ?></td>
                    <?php 
                            }
                        }
                    ?>
                </tr>
            <?php } ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="<?php pc_echo($attributeCount + 1); ?>">
                    <fieldset class="buttons">
                        <button class="add" type="submit" name="controller" value="StartAdd" title="<?php pc_echo(pc_message("listEntities.add.description", array(Pageley_Util_String::uncapitalize(pc_message("namespace." . $typeName))))); ?>"><?php pc_echo(pc_message("listEntities.add")); ?></button>
                        <button class="delete" type="submit" name="controller" value="StartDelete" title="<?php pc_echo(pc_message("listEntities.delete.description", array(Pageley_Util_String::uncapitalize(pc_message("namespace." . $typeName . ".plural"))))); ?>"><?php pc_echo(pc_message("listEntities.delete")); ?></button>
                    </fieldset>
                </td>
            </tr>
        </tfoot>
    </table>
</form>